package cn.whut.glab.web;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

import cn.whut.glab.domain.BD_Teacher;
import cn.whut.glab.domain.EasyUIPager;
import cn.whut.glab.service.TeacherService;

/**
 * Servlet implementation class TeacherListServlet
 */
public class TeacherListServlet extends HttpServlet {
	TeacherService service = new TeacherService();

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setHeader("Content-type", "text/html;charset=UTF-8"); 
		request.setCharacterEncoding("utf-8");
		String method=request.getParameter("method");
		if(method==null){
			//page：当前页   rows：每页的记录数 
			int page = Integer.parseInt(request.getParameter("page"));
			int rows = Integer.parseInt(request.getParameter("rows"));
			String name=request.getParameter("name");
			if(name==null){
				name="";
			}
			BD_Teacher teacher=new BD_Teacher();
			teacher.setName(name);
			try {
				EasyUIPager pager = service.getTeachList(rows, page,teacher);
				Gson gson=new Gson();
				response.getWriter().write(gson.toJson(pager));
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else if(method.equals("remove")){
			
			try {
				String Id=request.getParameter("Id");
				service.deleteTeacher(Id);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else if(method.equals("add")){
			String Name=request.getParameter("Name");
			String Gender=request.getParameter("Gender");
			String Birthday=request.getParameter("Birthday");
			String delBirthday=request.getParameter("delBirthday");
			String NativePlace=request.getParameter("NativePlace");
			String Tel=request.getParameter("Tel");
			String Email=request.getParameter("Email");
			String QQ=request.getParameter("QQ");
			BD_Teacher teacher=new BD_Teacher();
			teacher.setName(Name);
			teacher.setGender(Gender);
			teacher.setBirthday(Birthday);
			teacher.setNativePlace(NativePlace);
			teacher.setTel(Tel);
			teacher.setEmail(Email);
			teacher.setQQ(QQ);
			//System.out.println("add");
			//System.out.println(Name);
			try {
				service.addTeacher(teacher);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else if(method.equals("edit")){
			String Id=request.getParameter("Id");
			String Name=request.getParameter("Name");
			String Gender=request.getParameter("Gender");
			String Birthday=request.getParameter("Birthday");
			String delBirthday=request.getParameter("delBirthday");
			String NativePlace=request.getParameter("NativePlace");
			String Tel=request.getParameter("Tel");
			String Email=request.getParameter("Email");
			String QQ=request.getParameter("QQ");
			BD_Teacher teacher=new BD_Teacher();
			teacher.setName(Name);
			teacher.setGender(Gender);
			teacher.setBirthday(Birthday);
			teacher.setNativePlace(NativePlace);
			teacher.setTel(Tel);
			teacher.setEmail(Email);
			teacher.setQQ(QQ);		
			//System.out.println("edit");
			try {
				service.editTeacher(teacher, Id);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}
